package com.travel.server.manager.impl.command.contract;

import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.server.manager.dto.request.EmptyRequest;
import com.travel.server.manager.dto.request.contract.QueryContractUrlResponse;
import com.travel.server.manager.impl.bestsign.client.ApiTemplateInvoke;
import com.travel.server.manager.impl.bestsign.vo.BaseVo;
import com.travel.server.manager.impl.bestsign.vo.LinkVo;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.model.UserInfo;
import org.springframework.stereotype.Component;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/21 17:45
 * @Desc:
 */
@Component
public class QueryContractUrlCommand extends AbstractCommand<EmptyRequest, QueryContractUrlResponse> {
    @Override
    protected void checkInput(EmptyRequest request) {

    }

    @Override
    public QueryContractUrlResponse handle(EmptyRequest request) {
        UserInfo userInfo = getUserInfo();
        ApiTemplateInvoke invoke = new ApiTemplateInvoke();
        BaseVo<LinkVo> vo = invoke.queryDocUrl(userInfo.getAccount());
        if(vo == null){
            throw new WorkException(WorkCode.BEST_SIGN_API_ERROR);
        }
        if(ApiTemplateInvoke.SUCCESS.equals(vo.getCode())){
            LinkVo linkVo = vo.getVo();
            QueryContractUrlResponse response = new QueryContractUrlResponse();
            response.setUrl(linkVo.getValue());
            return response;
        }else{
            WorkCode otherSystem = WorkCode.OTHER_SYSTEM;
            otherSystem.setMessage(vo.getMessage());
            throw new WorkException(otherSystem);
        }
    }
}
